<!DOCTYPE html>
<html lang="zh-CN">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>获取字符串指定位置字符</title>
</head>

<body>
  <input type="text" id="inputStr" placeholder="请输入一串字符 s">
  <input type="number" id="inputIndex" placeholder="请输入数字 i">
  <button onclick="getCharAtPosition()">获取字符</button>
  <div id="result"></div>
  <script>
    function getCharAtPosition() {
      const str = document.getElementById('inputStr').value;
      const index = parseInt(document.getElementById('inputIndex').value);
      if (isNaN(index) || index < 0 || index >= str.length) {
        document.getElementById('result').innerHTML = "输入的数字不合法或者超出字符串索引范围";
      } else {
        const char = str.charAt(index);
        document.getElementById('result').innerHTML = `字符串中第 ${index} 个字符是：${char}`;
      }
    }
  </script>
</body>

</html>